package org.gcb.core.ui.nodes;

import java.beans.IntrospectionException;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import org.gcb.core.model.Defense;
import org.gcb.core.model.DefenseSet;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.BeanNode;
import org.openide.nodes.ChildFactory;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.lookup.Lookups;

/**
 *
 * @author Jason
 */
public class DefenseChildFactory extends ChildFactory<Defense>{
    static final Comparator<Defense> defenseComparator = new Comparator<Defense>() {

        @Override
        public int compare(Defense o1, Defense o2) {
            return o1.getDefenseType().compareTo(o2.getDefenseType());
        }
    };
    private TreeSet<Defense> defenseList;

    public DefenseChildFactory(TreeSet<Defense> defenseList) {
        this.defenseList = defenseList;
    }

    public DefenseChildFactory(DefenseSet defenses) {
        this.defenseList = new TreeSet<Defense>(defenseComparator);
        this.defenseList.addAll(defenses.values());
    }

    @Override
    protected boolean createKeys(List<Defense> toPopulate) {
        for (Defense defense : defenseList) {
            toPopulate.add(defense);
        }
        return true;
    }

    @Override
    protected Node createNodeForKey(Defense key) {
        Node node;
        try {
            node = new BeanNode(key);
        } catch (IntrospectionException ex) {
            node = new AbstractNode(Children.LEAF, Lookups.singleton(key));
        }
        node.setDisplayName(key.getText());
        node.setShortDescription(key.getDescription());
        return node;
    }

}
